<script setup>
import List from '../components/List.vue';
import ClusterNamespaceSelector from '../components/ClusterNamespaceSelector.vue'
import { getConfigMapListHandler as getListHandler, deleteConfigMapHandler as deleteHandler  } from '../../api/configmap.js';
import {reactive, computed, ref, toRefs} from 'vue'
import GenericOptions from '../components/GenericOptions.vue'
import { ElMessage,ElMessageBox  } from 'element-plus'
// import CodeMirror  from '../components/CodeMirror.vue'
import DialogYaml from '../components/DialogYaml.vue';
import { obj2Yaml } from '../../utils/utils';
import { useRouter } from 'vue-router';
const router = useRouter()
const data = reactive({
  items: [],
  clusterId: "",
  namespace: "",
  yamlContent: ""
})
// 定义一个查询Pod列表的函数
const getList = () => {
  getListHandler({clusterId: data.clusterId, namespace: data.namespace}).then(
    (res) => {
      data.items = res.data.data.items
      console.log("items:",data.items)
    }
  )
}
// 回调函数
const rollback = (clusterId, namespace)=> {
  data.clusterId = clusterId
  data.namespace = namespace
  getList()
}
const search = ref('')
const filterTableData = computed(() =>
  data.items.filter(
    (item) =>
      !search.value ||
      item.metadata.name.toLowerCase().includes(search.value.toLowerCase())
  )
)
// 展示详情
const showDetail = ref(false)
const detail = (row) => {
  const item = JSON.parse(JSON.stringify(row))
  // 删除managedFields
  delete item.metadata.managedFields
  // 把apiVersion kind补上去
  const itemtemp = {
    "apiVersion": "v1",
    "kind": "ConfigMap",
    "metadata": item.metadata,
    "data": item.data,
    "immutable": item.immutable,
  }
  // 把当前数据转成yaml
  const yamlData = obj2Yaml(itemtemp)
  data.yamlContent = yamlData
  showDetail.value = true
}
const {yamlContent,clusterId} = toRefs(data)
const handleChange = (value) => {
  console.log(value)
}
</script>

<template>
  <List title="ConfigMap列表">
    <template #header-options>
      <ClusterNamespaceSelector @namespaceChangedRollback="rollback">

      </ClusterNamespaceSelector>
      <el-input v-model="search" size="small" placeholder="搜索" style="width: 140px; margin-left: 10px; height: 32px;" />
    </template>
    <template #main>
      <el-table
      border 
      height="480" 
      :data="filterTableData" 
      style="width: 100%"
      :default-sort="{prop: 'metadata.creationTimestamp', order: 'descending'}"
      >
        <el-table-column fixed prop="" align="center" label="名称">
          <template #default="scope">
            <el-button link type="primary"  @click="detail(scope.row)">{{ scope.row.metadata.name }}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="immutable" align="center" label="只读" width="120" />
        <el-table-column prop="metadata.namespace" align="center" label="命名空间" width="240" />
        <el-table-column fixed="right" align="center" label="操作" width="320" >
          <template #default="scope">
            <GenericOptions
            v-on:delete-rollback="getList"
            :name="scope.row.metadata.name"
            :cluster-id="clusterId"
            :disabled ="scope.row.metadata.deletionTimestamp" 
            resource-type="ConfigMap"
            :namespace="scope.row.metadata.namespace"
            >
            </GenericOptions>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </List>
  <DialogYaml
  :cluster-id="data.clusterId"
  :namespace="data.namespace"
  :yaml-content="yamlContent"
  :show-dialog="showDetail"
  @close-dialog-handler="(val)=>{showDetail = val}"
  >

  </DialogYaml>
  <!-- <el-dialog
  v-model="showDetail" 
  width="70%">
  <template #header="{ titleId, titleClass }">
    <div class="my-header">
      <div :id="titleId" :class="titleClass" style="margin: 10px auto 0 auto;">
        <el-button type="primary" title="资源所在的集群" >
          <i class="iconfont icon-kubernetes" style="font-size: 20px;" />
          <span style="font-size: 20px;margin-left: 10px;">{{ data.clusterId }}</span>
        </el-button>
        <el-button type="primary" title="资源所在的命名空间" >
          <i class="iconfont icon-Namespace" style="font-size: 20px;" />
          <span style="font-size: 20px;margin-left: 10px;">{{ data.namespace }}</span>
        </el-button>
      </div>
    </div>
  </template>
    <CodeMirror
    v-model="yamlContent"
    ></CodeMirror>
  </el-dialog> -->
</template>

<style scoped>
.my-header {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
</style>
